import java.util.ArrayList;
import java.util.List;

public class Solution {
    String[] str = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    StringBuilder path = new StringBuilder();
    List<String> ret = new ArrayList<>();
    int len;

    public List<String> letterCombinations(String digits) {
        len = digits.length();

        if (len == 0) {
            return ret;
        }

        dfs(digits, 0);

        return ret;
    }

    public void dfs(String digits, int pos) {
        if (pos == len) {
            ret.add(path.toString());
            return;
        }

        String cur = str[digits.charAt(pos) - '0'];
        for (int i = 0; i < cur.length(); i++) {
            path.append(cur.charAt(i));
            dfs(digits, pos + 1);
            //回溯
            path.deleteCharAt(path.length() - 1);
        }
    }
}
